(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.limit.limitTree.listController', listController);
	listController.$inject = [ '$scope', '$q', '$filter', 'Constants', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService', 'limitService', 'commonService' ];
	function listController($scope, $q, $filter, Constants, gridService, modalService, layoutConfig, validatorConfig, authService, limitService, commonService) {
		var ctrl = this, info = $scope.$stateParams.params, log = $scope.loggerFactory.getLogger("limitList");
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		
		ctrl.init = function() {
			ctrl.setupDataStatusOptions = commonService.setupDataStatusOptions();
			ctrl.gridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, paginationChanged, rowSelectionChanged, sortChanged);
			ctrl.gridOptions.data = 'vm.searchCriteria.list';
			ctrl.searchCriteria = ctrl.gridOptions.defaultSearchCriteria;
			ctrl.searchCriteria.limitNodeId = $filter('encode')(info.id);// get selected limitNodeId
			if (info.parentLimitId && $filter('encode')(info.parentLimitId) > 0) {
				ctrl.searchCriteria.parentLimitId = $filter('encode')(info.parentLimitId);
			} else {
				delete ctrl.searchCriteria.parentLimitId;
			}
			ctrl.search();
			//
			if (!info.parentNodeId || $filter('encode')(info.parentLimitId) > 0) {
				ctrl.enableCreateButton = 'Y';
			} else {
				ctrl.enableCreateButton = 'N';
			}
		};

		ctrl.view = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			$scope.go('cda.limit.limitTree.view', ctrl.selectedRows[0]);
		};

		ctrl.create = function() {
			// get selected limitNodeId by subLimit button
			var params = {'limitNodeId' : info.id, 'parentLimitId' : info.parentLimitId};
			$scope.go('cda.limit.limitTree.create', params);
		};

		ctrl.update = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			$scope.go('cda.limit.limitTree.update', ctrl.selectedRows[0]);
		};

		ctrl.delete = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			$scope.go('cda.limit.limitTree.delete', ctrl.selectedRows[0]);
		};

		ctrl.search = function() {
			ctrl.searchCriteria.list = [];
			authService.getList(ctrl.searchCriteria).then(
			function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				gridService.updateGridOptions(ctrl.searchCriteria, ctrl.gridOptions);
			},angular.noop);
		};
		
		ctrl.subLimit = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			
			if (info.children && info.children.length > 0) {
				if (info.children.length > 1) {
					// params
					var __params = {'childLimitNodes' : info.children};
					modalService.openCustomDialogs({
						'title': 'Child LimitNode Edit',
						'dialogsModule' : 'cda.limit.childNodeEdit',
						'params': __params,
						'btnCallback' : {
							'ok' : function(limitNode) {							
								if (limitNode && limitNode.limitNodeId) {
									for (var i = 0; i < info.children.length; i++) {
										if (info.children[i].id == limitNode.limitNodeId) {
											var nextNodeInfo = info.children[i];
											nextNodeInfo.parentLimitId = ctrl.selectedRows[0].limitId;
//											var trueBranch = selectTrueBranch($scope.$parent.vm.limitData, nextNodeInfo);
//											trueBranch.parentLimitId = nextNodeInfo.parentLimitId;
//											$scope.$parent.vm.limitTreeApi.select_branch(trueBranch);
											$scope.go('cda.limit.limitTree.list', nextNodeInfo);
											break;
										}
									}
								}
							}
						}		
					});
				} else {
					var nextNodeInfo = info.children[0];
					nextNodeInfo.parentLimitId = ctrl.selectedRows[0].limitId;
					//select next branch
//					var trueBranch = selectTrueBranch($scope.$parent.vm.limitData, nextNodeInfo);
//					trueBranch.parentLimitId = nextNodeInfo.parentLimitId;
//					$scope.$parent.vm.limitTreeApi.select_branch(trueBranch);
					$scope.go('cda.limit.limitTree.list', nextNodeInfo);
				}
			} else {
				modalService.openErrorDialogs({
					messages : [ 'It has no sub-limit. To create sub-limit, please create a node first.' ]
				});
				return;
			}
		};
		
		function selectTrueBranch(limitData, nextNodeInfo) {
			for(var i =0 ; i < limitData.length ; i++){
				if(limitData[i].uid == nextNodeInfo.uid){
					return limitData[i];
				}else if(limitData[i].children && limitData[i].children.length > 0){
					var date = selectTrueBranch(limitData[i].children, nextNodeInfo);
					if(date != null){
						return date;
					};
				};
			};
		}

		function paginationChanged(pageNum, pageSize) {
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.search();
		}

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}

		function sortChanged(sortColumns) {
			// now just support sort by one column
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : '';
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : '';
			ctrl.search();
		}
	}
})(angular);
